import {
  ContactGateway,
  Contact,
  getContactDto,
  putContactDto,
  disableContactDto,
  postContactDto,
  ContactInterfaceWithDerive,
  PageData,
  PostMutiRes,
  PutRes,
  DisableRes,
  searchContactDto,
  switchContactDto,
} from "qqlx-core";

import { ViewBase, ViewBaseAction } from "../view-base";
import { ContactSchema } from "@/pond/schema-contact";

export class ViewContact extends ViewBase<Contact> implements ViewBaseAction<Contact>, ContactInterfaceWithDerive<{}> {
  private static instance: ViewContact;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewContact();
    }

    if (!this.instance) this.instance = new ViewContact();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Contact {
    return new ContactSchema();
  }

  async get(input: Pick<getContactDto, "groupId">) {
    this.pending();
    const dto = { ...input, page: this.data.entityListPage, conditionList: this.data.conditionList };
    const { list, total } = await this.$request.getByPut<getContactDto, PageData<Contact>>(ContactGateway.path, dto);
    this.data.entityListTotal = total;
    return { list, total };
  }

  async post(input: Pick<getContactDto, "groupId">, schemaList?: Contact[]) {
    this.pending();
    const dto = { ...input, schemaList: schemaList || this.data.schemaList };
    return this.$request.post<postContactDto, PostMutiRes<Contact>>(ContactGateway.path, dto);
  }

  async put() {
    this.pending();
    const dto = { schema: this.data.schema };
    return this.$request.put<putContactDto, PutRes<Contact>>(ContactGateway.path, dto);
  }

  async disable(dto: disableContactDto) {
    this.pending();
    return this.$request.delete<disableContactDto, DisableRes>(ContactGateway.path, dto);
  }

  /** 根据关键词，在全部授权中搜索客户 */
  async search(dto: searchContactDto) {
    this.pending();
    return this.$request.put<searchContactDto, PageData<Contact>>(`${ContactGateway.path}/search`, dto);
  }

  /** 切换分组 */
  async switch(dto: switchContactDto) {
    this.pending();
    return this.$request.put<switchContactDto, null>(`${ContactGateway.path}/switch`, dto);
  }
}
